package com.fankai.lease.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankai.lease.common.login.LoginUserHolder;
import com.fankai.lease.model.entity.*;
import com.fankai.lease.model.enums.ItemType;
import com.fankai.lease.web.app.mapper.*;
import com.fankai.lease.web.app.service.ApartmentInfoService;
import com.fankai.lease.web.app.service.BrowsingHistoryService;
import com.fankai.lease.web.app.service.RoomInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fankai.lease.web.app.vo.apartment.ApartmentItemVo;
import com.fankai.lease.web.app.vo.attr.AttrValueVo;
import com.fankai.lease.web.app.vo.fee.FeeValueVo;
import com.fankai.lease.web.app.vo.graph.GraphVo;
import com.fankai.lease.web.app.vo.room.RoomDetailVo;
import com.fankai.lease.web.app.vo.room.RoomItemVo;
import com.fankai.lease.web.app.vo.room.RoomQueryVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Resource
    private GraphInfoMapper graphInfoMapper;

    @Resource
    private AttrValueMapper attrValueMapper;

    @Resource
    private FacilityInfoMapper facilityInfoMapper;

    @Resource
    private LabelInfoMapper labelInfoMapper;

    @Resource
    private PaymentTypeMapper paymentTypeMapper;

    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private LeaseTermMapper leaseTermMapper;
    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;

    @Resource
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(roomItemVoPage, queryVo);
    }

    @Override
    public RoomDetailVo selectDetailById(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null) {
            return null;
        }
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, roomInfo.getApartmentId());
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        ArrayList<GraphVo> graphVoArrayList = new ArrayList<>();
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoArrayList.add(graphVo);
        });
        apartmentItemVo.setGraphVoList(graphVoArrayList);
        List<LabelInfo> labelApartInfoList = labelInfoMapper.selectLabelInfoListByApartId(roomInfo.getApartmentId());
        apartmentItemVo.setLabelInfoList(labelApartInfoList);
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartId(roomInfo.getApartmentId());
        apartmentItemVo.setMinRent(minRent);
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoListByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueVoListByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoVoListByRoomId(id);
        List<LabelInfo> labelRoomInfoList = labelInfoMapper.selectLabelInfoListByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeListByRoomId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoListByApartId(roomInfo.getApartmentId());
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermListByRoomId(id);

        roomDetailVo.setLabelInfoList(labelRoomInfoList);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        browsingHistoryService.saveHistory(LoginUserHolder.getLoginUser().getUserId(), id);
        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }
}




